package io.bsly.modules.app.service.localmaster.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.bsly.common.utils.PageUtils;
import io.bsly.common.utils.Query;
import io.bsly.modules.app.dao.localDao.DyHotelFavoriteDao;
import io.bsly.modules.app.entity.localEntity.DyHotelFavoriteEntity;
import io.bsly.modules.app.entity.sourceEntity.HotelEntity;
import io.bsly.modules.app.pojo.HotelFavoriteVo;
import io.bsly.modules.app.service.localmaster.DyHotelFavoriteService;
import io.bsly.modules.app.service.sourceDb.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("dyHotelFavoriteService")
public class DyHotelFavoriteServiceImpl extends ServiceImpl<DyHotelFavoriteDao, DyHotelFavoriteEntity> implements DyHotelFavoriteService {
    @Autowired
    private HotelService hotelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DyHotelFavoriteEntity> page = this.page(
                new Query<DyHotelFavoriteEntity>().getPage(params),
                new QueryWrapper<DyHotelFavoriteEntity>().lambda()
                .eq(params.containsKey("userId"),DyHotelFavoriteEntity::getUserId,params.get("userId"))
        );
        if(!CollectionUtils.isEmpty(page.getRecords())){
            List<Integer> collectHotelIds = page.getRecords().stream().map(one -> one.getHotelId()).collect(Collectors.toList());
            List<HotelEntity> listHotel = hotelService.list(new LambdaQueryWrapper<HotelEntity>()
                    .select(HotelEntity::getName, HotelEntity::getCity)//指定返回的字段
                    .in(HotelEntity::getId, collectHotelIds));

            List<HotelFavoriteVo> collectHotelFavorite = listHotel.stream().map(one -> {
                HotelFavoriteVo hotelFavoriteVo = new HotelFavoriteVo();
                hotelFavoriteVo.setHotelId(one.getId());
                hotelFavoriteVo.setCity(one.getCity());
                hotelFavoriteVo.setName(one.getName());
                return hotelFavoriteVo;
            }).collect(Collectors.toList());

            return new PageUtils(collectHotelFavorite,(int)page.getTotal(),(int)page.getSize(),(int)page.getCurrent());
        } else {
            return new PageUtils(new ArrayList<>(),(int)page.getTotal(),(int)page.getSize(),(int)page.getCurrent());
        }
    }
}